const MapKeys = require('../template')

console.log("=== MapKeys 性能测试 ===")

// 测试小对象映射键性能
console.log(
  "小对象映射键性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeys(obj, (value, key) => key.toUpperCase())
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次映射时间应该小于0.01ms
  })()
)

// 测试大对象映射键性能
console.log(
  "大对象映射键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeys(obj, (value, key) => key.toUpperCase())
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试深度映射键性能
console.log(
  "深度映射键性能:", (() => {
    const obj = { a: { b: { c: { d: { e: 1 } } } } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeysDeep(obj, (value, key) => key.toUpperCase(), 3)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次映射时间应该小于0.1ms
  })()
)

// 测试条件映射键性能
console.log(
  "条件映射键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeysWithCondition(obj, (value, key) => `new_${key}`, (value, key) => value > 500)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试类型映射键性能
console.log(
  "类型映射键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i % 2 === 0 ? i : `string${i}`
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeysWithType(obj, (value, key) => `num_${key}`, 'number')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试正则映射键性能
console.log(
  "正则映射键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`user_${i}`] = i
      obj[`admin_${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeysWithPattern(obj, (value, key) => key.replace(/^user_/, 'person_'), /^user_/)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试值映射键性能
console.log(
  "值映射键性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeysWithValue(obj, (value, key) => `key_${value}`, (value, key) => value * 2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试验证映射键性能
console.log(
  "验证映射键性能:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = { name: { type: 'string' }, age: { type: 'number' }, email: { type: 'string' } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        MapKeys.mapKeysWithValidation(obj, (value, key) => `valid_${key}`, schema)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次映射时间应该小于0.01ms
  })()
)

// 测试统计映射键性能
console.log(
  "统计映射键性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeysWithStats(obj, (value, key) => key.toUpperCase())
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次映射时间应该小于0.01ms
  })()
)

// 测试异步映射键性能
console.log(
  "异步映射键性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const iterations = 1000
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(MapKeys.mapKeysAsync(obj, (value, key) => key.toUpperCase(), async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value > 2
      }))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => typeof result === 'object') &&
        avgTime < 5 // 平均每次映射时间应该小于5ms
    }).catch(() => false)
  })()
)

// 测试并发映射键性能
console.log(
  "并发映射键性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const iterations = 100
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(MapKeys.mapKeysConcurrent(obj, (value, key) => `new_${key}`, async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value > 2
      }, 3))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => typeof result === 'object') &&
        avgTime < 10 // 平均每次映射时间应该小于10ms
    }).catch(() => false)
  })()
)

// 测试复杂结构映射键性能
console.log(
  "复杂结构映射键性能:", (() => {
    const obj = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}`, age: 20 + i },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          likes: j * 10
        }))
      })),
      meta: { total: 100, page: 1 }
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapKeys.mapKeys(obj, (value, key) => `api_${key}`)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次映射时间应该小于0.1ms
  })()
)

// 测试内存使用性能
console.log(
  "内存使用性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 10000
    const results = []

    for (let i = 0; i < iterations; i++) {
      const mapped = MapKeys.mapKeys(obj, (value, key) => key.toUpperCase())
      results.push(mapped)
    }

    return results.length === iterations &&
      results.every(result => typeof result === 'object' && result.A === 1 && result.B === 2 && result.C === 3)
  })()
)

console.log("=== MapKeys 性能测试完成 ===")
